<?php

/**
 * PARSER
 * mer.shanghai@gmail.com
 *
 */

/*
 == PHP_ARRAY ==
 version  插件版本
 addon    插件名称 
 name     活动名称
 start    活动开始
 end      活动结束 
 note     活动备注
 instance 活动副本
 
 玩家
 members => array (
  0 => array(name,race,class,level)
 )
 
 事件
 events => array (
  0 => array( name,score,boss,time,list=>array(player,player,player) )
 )
 
 物品
 items => array (
  0 => array (name,score,count,looter,time)
 )
*/

class Parser_report {

  var $config = array();
  var $races  = array('nightelf','human','gnome','draenei','dwarf','troll','scourge','orc','tauren','bloodelf');

  function __construct($config=array()) {
    $this->setconfig($config);
  }

  function __destruct() {
  }

  function setconfig($config=array()) {
    if (!empty($config) && is_array($config)){
      foreach($config as $key=>$value) {
        $this->config[$key] = $value;
      }
    }
  }

  function parser($xml) {
    $xml = strtolower($xml);
    $xml = preg_replace("/\s*<\s*/",'<',$xml);
    $xml = preg_replace("/\s*>\s*/",'>',$xml);
    $arr = xml2array($xml);
    return $arr;
  }
  
  //ctrt
  function parser_ctrt($xml) {
    $arr = $this->parser($xml);
    $return = array(
      'version'   => '0',
      'addon'     => 'CTRT',
      'name'      => trim($arr['zone']) ? trim($arr['zone']) : $this->get_time($arr['key']),
      'start'     => $this->get_time($arr['start']),
      'end'       => $this->get_time($arr['end']),      
      'note'      => $this->get_note($arr['note']),
      'instance'  => trim($arr['zone'])
    );
    $return['members'] = array();
    $return['events'] = array();
    $return['items'] = array();
    if(is_array($arr['playerinfos'])) {
      foreach($arr['playerinfos'] as $_=>$v) {
        $return['members'][] = array(
          $this->get_member($v['name']),
          $this->get_race($v['race']),
          $this->get_class($v['class']),
          $this->get_level($v['level'])
        );
      }
    }
    if(is_array($arr['bosskills'])) {
      foreach($arr['bosskills'] as $_=>$v) {
        
      }
    }
    if(is_array($arr['loot'])) {
      foreach($arr['loot'] as $_=>$v) {
        $return['items'][] = array(
          $this->get_color($v['color']) ."|".$this->get_itemid($v['itemid'])."|".trim($v['itemname']),
          $this->get_costs($v['costs']),
          $this->get_count($v['count']),          
          $this->get_member($v['player']),
          $this->get_time($v['time'])
        );
      }
    }
    return $return;
  }
  //merdkp
  function parser_merdkp($xml) {
    $arr = $this->parser($xml);
    $return = array(
      'version'   => trim($arr['version']),
      'addon'     => 'MERDKP',
      'name'      => trim($arr['name']),
      'start'     => $this->get_time($arr['start']),
      'end'       => $this->get_time($arr['end']),      
      'note'      => '',
      'instance'  => trim($arr['instance'])
    );
    $return['members'] = array();
    $return['events'] = array();
    $return['items'] = array();
    if(is_array($arr['members'])) {
      foreach($arr['members'] as $_=>$v) {
        $return['members'][] = array(
          $this->get_member($v['name']),
          $this->get_race($v['race']),
          $this->get_class($v['class']),
          $this->get_level($v['level'])
        );
      }
    }
    if(is_array($arr['events'])) {
      foreach($arr['events'] as $_=>$v) {
        $return['events'][] = array(
          trim($v['name']),
          $this->get_costs($v['score']),
          trim($v['boss']),          
          $this->get_time($v['time']),
          (array)$v['list']
        );
      }
    }
    if(is_array($arr['items'])) {
      foreach($arr['items'] as $_=>$v) {
        $return['items'][] = array(
          trim($v['name']),
          $this->get_costs($v['score']),
          $this->get_count($v['count']),          
          $this->get_member($v['looter']),
          $this->get_time($v['time'])
        );
      }
    }
    return $return;
  }
  //midkp
  function get_boss_midkp($v,$n) {
    $v = trim($v);
    if ($v=='1') {
      if(preg_match("/^完成 (.+)$/",$n,$return)) return trim($return[1]);
      else return '';
    }
    else { return $v;}
  }
  function get_time_midkp($v) {
    return $this->get_time(str_replace(".","-",$v));
  }
  function get_item_midkp($v) {
    if(preg_match("/(\w+).+?:(\d+).+?\[(.+)\]/",$v,$return)) {
      return $this->get_color($return[1]) .'|'. $return[2] .'|'. trim($return[3]);
    }
    else {
      return $v;
    }
  }
  function parser_midkp($xml) {
    $arr = $this->parser($xml);
    $arr = $arr['raid'];
    $return = array(
      'version'   => trim($arr['version']),
      'addon'     => 'MIDKP',
      'name'      => trim($arr['name']),
      'start'     => $this->get_time_midkp($arr['start']),
      'end'       => $this->get_time_midkp($arr['end']),      
      'note'      => '',
      'instance'  => trim($arr['places']['place'])
    );
    $return['members'] = array();
    $return['events'] = array();
    $return['items'] = array();
    if(is_array($arr['members']['member'])) {
      foreach($arr['members']['member'] as $_=>$v) {
        $return['members'][] = array(
          $this->get_member($v['name']),
          $this->get_race($v['race']),
          $this->get_class($v['class']),
          $this->get_level($v['level'])
        );
      }
    }
    if(is_array($arr['events']['event'])) {
      foreach($arr['events']['event'] as $_=>$v) {
        $return['events'][] = array(
          trim($v['name']),
          $this->get_costs($v['point']),
          $this->get_boss_midkp($v['boss'],$v['name']),          
          $this->get_time_midkp($v['time']),
          (array)$v['members']['member']
        );
      }
    }
    if(is_array($arr['items']['item'])) {
      foreach($arr['items']['item'] as $_=>$v) {
        $return['items'][] = array(
          $this->get_item_midkp($v['name']),
          $this->get_costs($v['point']),
          $this->get_count($v['count']),          
          $this->get_member($v['looter']),
          $this->get_time_midkp($v['time'])
        );
      }
    }
    return $return;
  }
  //report
  function parser_report($xml) {
    
  }
  //text
  function parser_text($xml) {
    
  }
  
  
  function get_time($v) {
    return is_numeric($v) ? $v : strtotime($v);
  }
  
  function get_member($v) {
    return ucfirst(strtolower(trim($v)));
  }
  
  function get_race($v) {
    return strtoupper($v);
  }
  
  function get_class($v) {
    return strtoupper($v);
  }
  
  function get_level($v) {
    return $v ? (int)$v : 80;
  }
  
  function get_itemid($v) {
    if(preg_match("/^(\d+):*/",$v,$return)) return $return[1];
    else return $v;
  }
  
  function get_note($v) {
    if(preg_match("/<\!\[cdata\[(.+?)\]\]>/",$v,$return)) return trim($return[1]);
    else return trim($v);
  }
 
  function get_count($v) {
    $v = (int)$v;
    return $v ? $v : 1;
  }
  
  function get_costs($v) {
    return (int)$v;
  }
  
  function get_color($v) {
    if(strlen($v)>6 && preg_match("/([a-fA-F0-9]{6})$/i",$v,$return)) return $return[1];
    else return $v;
  }
  
}

?>